var router = require('koa-router')();

//微信底部菜单
router.post('/weixin/setbutton',async (ctx,next) => {
	var {button} = ctx.request.body;
	deleteActive(button);

	ctx.body = await ctx.post('/wx/setupMenu',{json:JSON.stringify({button})});
});

//获取微信底部菜单现有配置
router.post('/weixin/getbutton',async (ctx,next) => {
	ctx.body = await ctx.post('/wx/getMenu');
});

//关键词设置
router.post('/weixin/postKeysMsg',async (ctx,next) => {
	let postData = ctx.request.body;

	let data = await ctx.post('/wx/postKeysMsg', postData);
	ctx.body = data;
});

//关注时设置
router.post('/weixin/postInitMsg',async (ctx,next) => {
	let postData = ctx.request.body;
	let data = await ctx.post('/wx/postInitMsg', postData);
	ctx.body = data;
});

//获取关注时设置项
router.post('/weixin/getInitMsg',async (ctx,next) => {
	let data = await ctx.post('/wx/getInitMsg');
	ctx.body = data;
});

router.post('/weixin/getKeysMsg',async (ctx,next) => {
	let data = await ctx.post('/wx/getKeysMsg');
	ctx.body = data;
});

router.post('/weixin/delKeysMsg',async (ctx,next) => {
	let {reply_name} = ctx.request.body;
	let data = await ctx.post('/wx/delKeysMsg', {reply_name});
	ctx.body = data;
});

router.post('/weixin/addMsg',async (ctx,next) =>{
	let {params} = ctx.request.body;
	console.log('/weixin/addMsg==========params',params);
	let result  = await ctx.post('/admin/wxConfig/add',{...params});
	console.log('/weixin/addMsg==========result',result);
	ctx.body = result;
})

router.post('/weixin/getMsg',async(ctx,next) => {
	let result = await ctx.post('/admin/wxConfig/list',{});
	console.log('/weixin/getMsg===========',result);
	ctx.body = result;
})

router.post('/weixin/delete',async(ctx,next) => {
	let result = await ctx.post('/admin/wxConfig/delete',{});
	console.log('/weixin/delete=====',result);
	ctx.body = result;
})

function deleteActive(button){
	button.forEach((item,i)=>{
        delete item.active ;
        if(!item.sub_button){
        	item.sub_button = []
        }
        if(item.sub_button.length>0){
            deleteActive(item.sub_button)
        }

    })
};


export default router;
